package com.lzx.iteam;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.*;
import com.lzx.iteam.adapter.PayTeamAdapter;
import com.lzx.iteam.adapter.PayToolsGroupAdapter;
import com.lzx.iteam.base.FindViewById;
import com.lzx.iteam.base.MyBaseActivity;
import com.lzx.iteam.bean.PayToolsData;
import com.lzx.iteam.bean.PayToolsGroupData;
import com.lzx.iteam.net.AsynHttpClient;
import com.lzx.iteam.net.LocalLogin;
import com.lzx.iteam.net.PayToolsGroupMsg;
import com.lzx.iteam.util.AllDialogUtil;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * **********************************
 * User: zhangshuai
 * Date: 2016年 09月 18日
 * Time: 上午10:12
 *
 * @QQ : 1234567890
 * **********************************
 */
public class PayToolsTeamActivity extends MyBaseActivity implements View.OnClickListener{

    @FindViewById(id = R.id.title_tv_right)
    private TextView mTvRight;
    @FindViewById(id = R.id.title_tv_center)
    private TextView mTvTitle;
    @FindViewById(id = R.id.title_ll_back)
    private LinearLayout mLayoutBack;
    @FindViewById(id = R.id.pay_tools_tv)
    private TextView mTvPay;
    @FindViewById(id = R.id.pay_tools_lv)
    private ListView mLvPay;

    private int mPosition;

    private PayToolsData mPayToolsData;
    private ArrayList<PayToolsGroupData> mDatas;
    private PayToolsGroupAdapter mAdapter;
    private Dialog mWaitDialog;


    public static  final int GET_PAY_TOOLS_GROUP = 10000;

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case GET_PAY_TOOLS_GROUP:
                    httpReqDlgDismiss();
                    if (msg.arg1 == 0){
                        mDatas = (ArrayList<PayToolsGroupData>) msg.obj;
                        mAdapter.bindData(mPayToolsData.getType(),mDatas);
                    }else{
                        Toast.makeText(PayToolsTeamActivity.this, (String)msg.obj, Toast.LENGTH_SHORT).show();
                    }
                    break;
            }

        }
    };


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState, R.layout.pay_tools_team_layout);
    }

    @Override
    public void initListener() {
        mLayoutBack.setOnClickListener(this);
    }

    @Override
    public void initData() {

        mPayToolsData = (PayToolsData) getIntent().getSerializableExtra("payToolsData");
        mTvTitle.setText(mPayToolsData.getName());
        mTvPay.setText(mPayToolsData.getPrice() +" 付费升级");

        httpReqDlgShow();
        getGroupData();
        mAdapter = new PayToolsGroupAdapter(mActivity);
        mLvPay.setAdapter(mAdapter);
        mLvPay.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mPosition = position;
                mAdapter.updateView(mPosition);
            }
        });

    }

    private void getGroupData() {

        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin.getInstance().mSid));
        PayToolsGroupMsg msg = new PayToolsGroupMsg(mHandler.obtainMessage(GET_PAY_TOOLS_GROUP));
        msg.mApi = AsynHttpClient.API_PERMISSION_GROUP_LIST;
        msg.mParams = nvps;
        AsynHttpClient.getInstance(mActivity).execUserHttp(msg);

    }

    /**
     * 显示等待的dialog
     */
    private void httpReqDlgShow() {
        if (mWaitDialog == null) {
            mWaitDialog = AllDialogUtil.getInstance(mActivity).waitDialog();
//            mProgressDialog.setCancelable(false);
            mWaitDialog.show();
        }

    }

    /**
     * 取消等待的dialog
     */
    private void httpReqDlgDismiss() {
        if (mWaitDialog != null && mWaitDialog.isShowing()) {
            mWaitDialog.dismiss();
            mWaitDialog = null;
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_ll_back:
                finish();
                break;
            case R.id.pay_tools_tv:
                break;

        }
    }
}
